package com.gusto.engine.colfil.neighborhood.storedistance;

import java.util.List;

import org.apache.log4j.Logger;

import com.gusto.engine.colfil.Distance;
import com.gusto.engine.colfil.Prediction;
import com.gusto.engine.colfil.Rating;
import com.gusto.engine.colfil.neighborhood.ColEventListener;
import com.gusto.engine.colfil.neighborhood.ColNeighborhoodService;

/**
 * <p>This implementation stores the rating and all the distances 
 * that need to be recalculated using the {@link DistanceProcessor}.</p>
 * 
 * @author amokrane.belloui@gmail.com
 * 
 */
public class CollaborativeNeighborhoodServiceImpl1 implements ColNeighborhoodService, ColEventListener {
	
	private Logger log = Logger.getLogger(getClass());
	
	private DistanceProcessor distanceProcessor;
	private DistancePersistenceDAO distanceDAO;
	
	public void setDistanceDAO(DistancePersistenceDAO distanceDAO) {
		this.distanceDAO = distanceDAO;
	}
	
	public void setDistanceProcessor(DistanceProcessor distanceProcessor) {
		this.distanceProcessor = distanceProcessor;
	}
	
	public void ratingInserted(long userId, long itemId, Rating rating, boolean checkExistence) throws Throwable {
		if (distanceProcessor != null) {
			log.info("Process Rating " + rating);
			distanceProcessor.insertRating(userId, itemId, rating, checkExistence);
		}
	}
	
	public void predictionInserted(long userId, long itemId, Prediction prediction) throws Throwable {
		if (distanceProcessor != null) {
			log.info("Process Prediction " + prediction);
			distanceProcessor.insertPrediction(userId, itemId, prediction);
		}
	}
	
	public List<Distance> getNearUsers(long userId) {
		log.info("Get near users");
		return distanceDAO.getNearUsers(userId);
	}
	
	public List<Distance> getNearItems(long itemId) {
		log.info("Get near items");
		return distanceDAO.getNearItems(itemId);
	}
	
	public List<Distance> getNearUsers(long userId, String formula, int MIN_SHARED_ITEM, double MIN_SIMILARITY) {
		log.info("Get near users");
		return distanceDAO.getNearUsers(userId, formula, MIN_SHARED_ITEM, MIN_SIMILARITY);
	}
	
	public List<Distance> getNearItems(long itemId, String formula, int MIN_SHARED_ITEM, double MIN_SIMILARITY) {
		log.info("Get near items");
		return distanceDAO.getNearItems(itemId, formula, MIN_SHARED_ITEM, MIN_SIMILARITY);
	}
	
}
